package com.ytjj.qmyx.mall.controller;

import com.alibaba.fastjson.JSONObject;
import com.ytjj.common.api.CommonResult;
import com.ytjj.common.controller.BaseController;
import com.ytjj.common.model.UsersResponse;
import com.ytjj.common.service.RedisService;
import com.ytjj.common.utils.TokenUtil;
import com.ytjj.qmyx.mall.constants.RedisConstants;
import com.ytjj.qmyx.mall.enums.HelpEnums;
import com.ytjj.qmyx.mall.model.HelpWork;
import com.ytjj.qmyx.mall.model.UsersFake;
import com.ytjj.qmyx.mall.model.request.*;
import com.ytjj.qmyx.mall.model.response.*;
import com.ytjj.qmyx.mall.service.EvaluationServive;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 测评相关接口
 *
 *
 * @version 1.0
 * 
 */
@RestController
@RequestMapping("/evaluation")
@Slf4j
public class EvaluationController  extends BaseController {

    @Resource
    private EvaluationServive evaluationServive;


    @Autowired
    private RedisService redisService;

    /**
     * 获取首页数据
     * @return
     */
    @GetMapping("/getHomeData")
    public CommonResult getHomeData(@RequestParam(value = "type") String type,
                                    @RequestParam(value = "pageNum") Integer pageNum,
                                    @RequestParam(value = "pageSize") Integer pageSize){
        UsersResponse currentUser = this.getH5CurrentUser();
        Map<String,Object> result = evaluationServive.selectHomeData(type,pageNum,pageSize,currentUser);
        return CommonResult.success(result);
    }

    /**
     * 发布测评
     */
    @PostMapping("release")
    public CommonResult release(@RequestBody EvaluationReleaseRequest request) {
        UsersResponse currentUser = this.getCurrentUser();
        request.setUserId(currentUser.getId());
        request.setUserName(currentUser.getNickname());
        request.setHeadImg(currentUser.getHeadImg());
        return CommonResult.success(evaluationServive.release(request));
    }

    /**
     * 我的购买/他的购买
     * @param pageNum
     * @param pageSize
     * @param allFlag null或者1-全部测评 2-未发布测评
     * @return
     */
    @GetMapping("/myShopping")
    public CommonResult getMyShopping(@RequestParam(required = false) Integer userId,
                                      @RequestParam(required = false) Integer status,
                                      @RequestParam(required = false) Integer allFlag,
                                      @RequestParam(value = "pageNum") Integer pageNum,
                                      @RequestParam(value = "pageSize") Integer pageSize){
        if(userId == null){
            UsersResponse currentUser = this.getH5CurrentUser();
            if(currentUser != null) {
                userId = currentUser.getId();
            }
        }
        if(userId == null) {
            return CommonResult.success();
        }
        return CommonResult.success(evaluationServive.getMyShopping(status,userId,pageNum,pageSize, allFlag));
    }

    /**
     * 根据动态id查询动态信息
     * @return
     */
    @GetMapping("/getDynamicInfo")
    public CommonResult getDynamicInfo(@RequestParam(value = "dynamicId") Integer dynamicId,
                                       @RequestParam(value = "pageNum") Integer pageNum,
                                       @RequestParam(value = "pageSize") Integer pageSize){
        List<DynamicEsResponse> DynamicEsDtoList = evaluationServive.getDynamicInfo(dynamicId,pageNum,pageSize);
        return CommonResult.success(DynamicEsDtoList);
    }

    /**
     * 根据动态id查询测评商品详情 (废弃)
     * @return
     */
    @GetMapping("/getProductInfo")
    public CommonResult getProductInfo(@RequestParam(value = "id") Integer id){
        EvaluationProductResponse evaluationProductResponse = evaluationServive.getProductInfo(id);
        // 规格列表
        evaluationProductResponse.setSpecMap(evaluationServive.getProductSpecList(id).stream()
                .collect(Collectors.toMap(item -> {
                    String skuSpec = item.getSkuSpec();
                    String[] specList = skuSpec.split(",");
                    StringBuilder stringBuilder = new StringBuilder();
                    for (int i = 0; i < specList.length; i++) {
                        String key = specList[i].split(":")[1];
                        if (i == specList.length - 1) {
                            stringBuilder.append(key);
                        } else {
                            stringBuilder.append(key).append("-");
                        }
                    }
                    return stringBuilder.toString();
                }, item -> item)));
        return CommonResult.success(evaluationProductResponse);
    }

    /**
     * 根据品牌id查询品牌信息
     * @return
     */
    @GetMapping("/getBrandData")
    public CommonResult getBrandData(@RequestParam(value = "bankerId") Integer bankerId){
        UsersResponse currentUser = this.getH5CurrentUser();
        BrandDataResponse brandDataResponse = evaluationServive.getBrandData(currentUser, bankerId);
        if (currentUser == null) {
            brandDataResponse.setIsFollow(false);
        } else {
            Boolean isFollow = false;
//            if (!"h5".equals(TokenUtil.getSource())) {
                isFollow = evaluationServive.selectIsFollow(bankerId, currentUser.getId());
//            }
            brandDataResponse.setIsFollow(null == isFollow ? false : isFollow);
        }

        return CommonResult.success(brandDataResponse);
    }

    /**
     * 根据供应商id、栏目id查询 清仓特卖  、 品质好货  、 0元测评
     * @return
     */
    @GetMapping("/getBrandProductList")
    public CommonResult getBrandProductList(@RequestParam(value = "bankerId") Integer bankerId,
                                     @RequestParam(value = "bankerColumnId",required = false) Integer bankerColumnId,
                                     @RequestParam(value = "pageNum") Integer pageNum,
                                     @RequestParam(value = "pageSize") Integer pageSize,
                                     @RequestParam (value = "productName",required = false) String productName){
        UsersResponse usersResponse = this.getH5CurrentUser();
        List<EvaluationProductResponse> evaluationProductList = evaluationServive.
                getBrandProductList(bankerId,bankerColumnId,pageNum,pageSize,productName,usersResponse);
        return CommonResult.success(evaluationProductList);
    }

    /**
     * 根据供应商id 查询 品质好货  、 0元测评
     * @return
     */
    @GetMapping("/queryQualityProductList")
    public CommonResult queryQualityProductList(@RequestParam(value = "bankerId") Integer bankerId){
        UsersResponse usersResponse = this.getH5CurrentUser();
        Map<String,Object> result = evaluationServive.
                queryQualityProductList(bankerId,usersResponse);
        return CommonResult.success(result);
    }

    /**
     * 根据供应商id 查询 清仓特卖
     * @return
     */
    @GetMapping("/queryClearProductList")
    public CommonResult getBrandProductList2(@RequestParam(value = "bankerId") Integer bankerId,
                                            @RequestParam(value = "pageNum") Integer pageNum,
                                            @RequestParam(value = "pageSize") Integer pageSize){
        List<EvaluationProductResponse> evaluationProductList = evaluationServive.
                queryClearProductList(bankerId,pageNum,pageSize);
        return CommonResult.success(evaluationProductList);
    }

    /**
     * 测评列表 （只查询0元测评商品的）
     * @return
     */
    @GetMapping("/queryEvaluationProductList")
    public CommonResult queryEvaluationProductList(@RequestParam(value = "pageNum") Integer pageNum,
                                                   @RequestParam(value = "pageSize") Integer pageSize,
                                                   @RequestParam(value = "soldOut",required = false,defaultValue = "1") Integer soldOut){
        UsersResponse usersResponse = this.getH5CurrentUser();
        List<EvaluationProductResponse> evaluationProductList = evaluationServive.queryEvaluationProductList(pageNum,pageSize,usersResponse,soldOut);
        return CommonResult.success(evaluationProductList);
    }

    @GetMapping("getZeroEvaluationNum")
    public CommonResult getZeroEvaluationNum() {
        return CommonResult.success(evaluationServive.getZeroEvaluationNum());
    }

    /**
     * 关注/取消关注
     * @return
     */
    @GetMapping("/updateBankerFollow")
    public CommonResult updateBankerFollow(@RequestParam Integer bankerId){
        UsersResponse currentUser = this.getCurrentUser();
        return CommonResult.success(evaluationServive.updateBankerFollow(bankerId,currentUser.getId()));
    }

    /**
     * 获取专栏列表
     * @return
     */
    @GetMapping("/getSpecialList")
    public CommonResult getSpecialList(){
        return CommonResult.success(evaluationServive.getSpecialList());
    }

    /**
     * 获取品牌里的专区
     * @return
     */
    @GetMapping("/getSpecialListByApp")
    public CommonResult getSpecialListByApp(){
        return CommonResult.success(evaluationServive.getSpecialListByApp());
    }

    /**
     * 根据专区id去查询品牌列表
     * @return
     */
    @GetMapping("/queryBankerListById")
    public CommonResult queryBankerListById(@RequestParam Integer specialId){
        List<BankerSiteInfoResponse> bankerSiteInfoList = evaluationServive.queryBankerListById(specialId);
        return CommonResult.success(bankerSiteInfoList);
    }


    /**
     * 我的测评/他的测评m
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/myEvaluation")
    public CommonResult getMyEvaluation(@RequestParam(required = false) Integer userId,
                                        @RequestParam(value = "pageNum") Integer pageNum,
                                        @RequestParam(value = "pageSize") Integer pageSize) {
        UsersResponse currentUser = this.getH5CurrentUser();
        return CommonResult.success(evaluationServive.getMyEvaluation(userId,pageNum,pageSize,currentUser));
    }

    /**
     * 添加助力单
     * @param request
     */
    @PostMapping("addAssistOrder")
    public CommonResult addAssistOrder(@Valid @RequestBody AddAssistOrderRequest request) {
        Integer userId = this.getCurrentUser().getId();
        request.setUserId(userId);
        evaluationServive.addAssistOrder(request);
        return CommonResult.success();
    }

    /**
     * app获取助力单
     */
    @GetMapping("getAssistOrder")
    public CommonResult getAssistOrder() {
        Integer userId = null;
        try {
            userId = this.getCurrentUser().getId();
        } catch (Exception e) {
            return CommonResult.success();
        }
        return CommonResult.success(evaluationServive.getAssistOrder(userId));
    }

    /**
     * app助力单状态在助力中时,并且库存为0时,将助力单状态变为失效
     */
    @GetMapping("getHelpWorkStatusWithStockZero")
    public CommonResult getHelpWorkStatusWithStockZero() {
        Integer userId = null;
        try {
            userId = this.getCurrentUser().getId();
        } catch (Exception e) {
            return CommonResult.success();
        }
        return CommonResult.success(evaluationServive.getHelpWorkStatusWithStockZero(userId));
    }

    /**
     * 小程序获取分享助力单
     */
    @GetMapping("getShareAssistOrder")
    public CommonResult getShareAssistOrder(@RequestParam Integer id) {
        return CommonResult.success(evaluationServive.getShareAssistOrder(this.getH5CurrentUser(), id));
    }


    /**
     * 用户协助助力单
     * @return
     */
    @GetMapping("userHelp")
    public CommonResult userHelp(UserHelpRequest userHelpRequest) {
        UsersResponse currentUser = this.getCurrentUser();
        userHelpRequest.setUserId(currentUser.getId());
        userHelpRequest.setTel(currentUser.getTel());
        return CommonResult.success(evaluationServive.userHelp(userHelpRequest));
    }

    /**
     * 获取我的关注 我的品牌
     * @return
     */
    @GetMapping("getFansBanker")
    public CommonResult getFansBanker(@RequestParam(required = false) Integer userId,
                                      @RequestParam(value = "type") Integer type) {
        if(userId == null){
            try{
                userId = this.getCurrentUser().getId();
                //没有登录直接返回空
            }catch (Exception e) {
                return CommonResult.success(new FansBankerResponse());
            }
        }
        return CommonResult.success(evaluationServive.getFansBanker(type, userId));
    }

    /**
     * 获取我的关注列表
     * @param userId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/getFansFollowList")
    public CommonResult getFansFollowList(@RequestParam(required = false) Integer userId,
                                        @RequestParam(value = "pageNum") Integer pageNum,
                                        @RequestParam(value = "pageSize") Integer pageSize) {
        if(userId == null) {
            userId = this.getCurrentUser().getId();
        }
        return CommonResult.success(evaluationServive.getFansFollowList(userId,pageNum,pageSize));
    }

    /**
     * 获取我关注的人数
     * @return
     */
    @GetMapping("/getMyIdolNum")
    public CommonResult getMyIdolNum(@RequestParam(required = false) Integer userId) {
//        UsersResponse usersResponse = null;
//        try {
//            if (null == userId) {
//                usersResponse = getCurrentUser();
//                userId = usersResponse.getId();
//            }
//        } catch (Exception e) {
//            log.info("===未登录用户===");
//        }
        if (null == userId) {
            return CommonResult.success(0);
        }
        Integer num = evaluationServive.getMyIdolNum(userId);
        return CommonResult.success(null == num ? 0 : num);
    }

    /**
     * 首页商品搜索
     * @param keyword
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("homePageSearch")
    public CommonResult homePageSearch(@RequestParam(required = false) String keyword,
                                       @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                       @RequestParam(value = "pageSize",required = false,defaultValue = "20") Integer pageSize,
                                       @RequestParam(value = "type") Integer type,
                                       @RequestParam(required = false) Integer productId,
                                       @RequestParam(required = false) Integer productType) {
        return CommonResult.success(evaluationServive.homePageSearch(productId, productType,type,keyword,pageNum,pageSize));
    }

    @GetMapping("homePageTypeProduct")
    public CommonResult getHomePageTypeProduct (@RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                                @RequestParam(value = "pageSize",required = false,defaultValue = "20") Integer pageSize,
                                                @RequestParam(value = "type") Integer type) {
        return CommonResult.success(evaluationServive.homePageTypeProduct(pageNum,pageSize,type));
    }

    /**
     * 我要点评
     */
    @GetMapping("commentInfo")
    public CommonResult commentInfo (@RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                                @RequestParam(value = "pageSize",required = false,defaultValue = "20") Integer pageSize,
                                                @RequestParam(value = "orderInfoId") Integer orderInfoId) {
        CommentInfoResponse commentInfoResponse = evaluationServive.commentInfo(pageNum, pageSize, orderInfoId);
        List<Object> fakeUserList = redisService.range(RedisConstants.FAKE_USER, 0, -1);
        Collections.shuffle(fakeUserList);
        List<String> headImg = fakeUserList.stream()
                .map(userItem -> JSONObject.parseObject((String) userItem, UsersFake.class))
                .map(usersFake -> usersFake.getHeadImg())
                .collect(Collectors.toList());
        commentInfoResponse.setHeadImg(headImg);
        return CommonResult.success(commentInfoResponse);
    }

    /**
     * 推荐/不推荐
     */
    @GetMapping("recommend")
    public CommonResult recommend (@RequestParam(value = "orderInfoId") Integer orderInfoId,
                                   @RequestParam(value = "isRecommend") Boolean isRecommend) {
        UsersResponse user = this.getCurrentUser();
        evaluationServive.recommend(orderInfoId,isRecommend,user.getId(),user.getTel());
        return CommonResult.success();
    }

    /**
     * 发布测评 （v2）
     */
    @PostMapping("releaseEvaluation")
    public CommonResult releaseEvaluation(@RequestBody EvaluationReleaseRequest request) {
        UsersResponse currentUser = this.getCurrentUser();
        request.setUserId(currentUser.getId());
        request.setUserName(currentUser.getNickname());
        request.setHeadImg(currentUser.getHeadImg());
        request.setTel(currentUser.getTel());
        return CommonResult.success(evaluationServive.releaseEvaluation(request));
    }

    // ------------------------------新版测评相应接口--------------------------

    /**
     * 测评栏目列表
     * @return
     */
    @GetMapping("specialColumn")
    public CommonResult specialColumn(){
        return CommonResult.success(evaluationServive.specialColumn());
    }

    /**
     * 0元栏目列表
     * @return
     */
    @GetMapping("specialMallColumn")
    public CommonResult specialMallColumn(){
        return CommonResult.success(evaluationServive.specialMallColumn());
    }


    /**
     * 专区id
     * @param specialId
     * @return
     */
    @GetMapping("evaluationHome")
    public CommonResult evaluationHome(@RequestParam Integer specialId,
                                       @RequestParam(value = "type" ,required = false) String type,
                                       @RequestParam(value = "fileType",required = false) String fileType,
                                       @RequestParam(value = "pageNum") Integer pageNum,
                                       @RequestParam(value = "pageSize") Integer pageSize,
                                       @RequestParam(value = "pageSize",required = false) Integer shuffle,
                                       @RequestParam(value = "categoryId" ,required = false) String categoryId){
        UsersResponse h5CurrentUser = null;
        try {
            h5CurrentUser = this.getCurrentUser();
        } catch (Exception e) {
            log.error("===没有登录信息===");
        }

        String platformData = TokenUtil.getPlatformData();
        EvaHomeResponse response = evaluationServive.evaluationHome(specialId,pageNum,pageSize,
                h5CurrentUser,type,fileType,platformData,shuffle,categoryId);
        return CommonResult.success(response);
    }

    /**
     * 零元测评
     * @param specialId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("zeroProduct")
    public CommonResult zeroProduct(@RequestParam Integer specialId,
                                       @RequestParam(value = "pageNum") Integer pageNum,
                                       @RequestParam(value = "pageSize") Integer pageSize,
                                    @RequestParam(value = "soldOut",required = false,defaultValue = "1") Integer soldOut){
        UsersResponse currentUser = this.getH5CurrentUser();
        return CommonResult.success(evaluationServive.zeroProduct(specialId,pageNum,pageSize,currentUser,soldOut));
    }

    /**
     * 严选官
     * @param type
     * @return
     */
    @GetMapping("querySelection")
    public CommonResult querySelection(@RequestParam(value = "type") String type,
                                       @RequestParam(value = "pageNum") Integer pageNum,
                                       @RequestParam(value = "pageSize") Integer pageSize){
        UsersResponse currentUser = this.getH5CurrentUser();
        return CommonResult.success(evaluationServive.querySelection(type,pageNum,pageSize,currentUser));
    }

    /**
     * 严选管列表
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("qualityUser")
    public CommonResult qualityUser(@RequestParam(value = "pageNum") Integer pageNum,
                                       @RequestParam(value = "pageSize") Integer pageSize){
        UsersResponse currentUser = this.getH5CurrentUser();
        return CommonResult.success(evaluationServive.qualityUser(pageNum,pageSize,currentUser));
    }

    /**
     * 搜索动态视频
     * @param name
     * @return
     */
    @GetMapping("searchDynamicData")
    public CommonResult searchDynamicData(@RequestParam(value = "name") String name,
                                          @RequestParam(value = "type") String type,
                                          @RequestParam(value = "fileType") String fileType,
                                          @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                          @RequestParam(value = "pageSize",required = false,defaultValue = "20") Integer pageSize){
        UsersResponse currentUser = this.getH5CurrentUser();
        return CommonResult.success(evaluationServive.searchDynamicData(name,type,fileType,pageNum,pageSize,currentUser));
    }

    /**
     * 搜索商城商品
     * @param request
     * @return
     */
    @PostMapping("/searchProductData")
    public CommonResult searchProductData(@RequestBody SearchProductRequest request) {
        return CommonResult.success(evaluationServive.searchProductData(request.getName(), request.getPageNum(), request.getPageSize(),request.getMark()));
    }

    /**
     * 搜索用户
     * @param name     用户名称
     * @param pageNum  页码
     * @param pageSize 页大小
     * @return
     */
    @GetMapping("/searchUsersData")
    public CommonResult searchUsersData(@RequestParam(value = "name") String name,
                                          @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                                          @RequestParam(value = "pageSize",required = false,defaultValue = "20") Integer pageSize) {
        UsersResponse user = null;
        try {
            user = this.getCurrentUser();
        }catch (Exception e) {}
        return CommonResult.success(evaluationServive.searchUsersData(name, pageNum, pageSize, null == user ? null : user.getId()));
    }

    /**
     * 推荐/不推荐 加上测评价格/质量等
     */
    @PostMapping("recommendAndStar")
    public CommonResult recommendAndStar (@RequestBody RecommendRequest recommendRequest) {
        UsersResponse user = this.getCurrentUser();
        evaluationServive.recommendAndStar(recommendRequest, user.getId(), user.getTel());
        return CommonResult.success();
    }

    /**
     * 测评详情
     */
    @GetMapping("evaluationDetails")
    public CommonResult evaluationDetails (@RequestParam(value = "dynamicId") Integer dynamicId,
                                           @RequestParam(value = "pageNum", required = false, defaultValue = "0") Integer pageNum) {
        UsersResponse user = null;
        try {
            user = this.getCurrentUser();
        }catch (Exception e) {}
        EvaluationDetailsResponse data = evaluationServive.evaluationDetails(dynamicId,pageNum, user);
        return CommonResult.success(data);
    }

    /**
     * 更多评论
     */
    @GetMapping("moreComment")
    public CommonResult moreComment (@RequestParam(value = "dynamicId") Integer dynamicId,
                                     @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        UsersResponse user = this.getCurrentUser();
        EvaluationDetailsResponse data = evaluationServive.moreComment(dynamicId, user, pageNum, pageSize);
        return CommonResult.success(data);
    }

    /**
     * 同款测评
     */
    @GetMapping("sameEvaluation")
    public CommonResult sameEvaluation (@RequestParam(value = "dynamicId") Integer dynamicId,
                                           @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                           @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        UsersResponse user = this.getCurrentUser();
        EvaluationDetailsResponse data = evaluationServive.sameEvaluation(dynamicId, user, pageNum, pageSize);
        return CommonResult.success(data);
    }

    /**
     * 发布页
     */
    @PostMapping("releasePage")
    public CommonResult releasePage(@RequestBody EvaluationReleaseRequest request) {
        UsersResponse currentUser = this.getCurrentUser();
        request.setUserId(currentUser.getId());
        request.setUserName(currentUser.getNickname());
        request.setHeadImg(currentUser.getHeadImg());
        request.setTel(currentUser.getTel());
        return CommonResult.success(evaluationServive.releasePage(request));
    }


    @GetMapping("/getForumLive")
    public CommonResult getForumLive(@RequestParam(value = "id") Integer id){
        String platformData=TokenUtil.getPlatformData();
        return CommonResult.success(evaluationServive.getForumLive(id,platformData));
    }

    @GetMapping("/getForumLiveInfoList")
    public CommonResult getForumLiveInfoList(@RequestParam(value = "id") Integer id,
                                     @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                     @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize){
        String platformData=TokenUtil.getPlatformData();
        return CommonResult.success(evaluationServive.getForumLiveInfoList(id,pageNum,pageSize,platformData));
    }

    /**
     * 添加助力单
     * 并返回id
     * @param request
     */
    @PostMapping("addAssistOrderId")
    public CommonResult addAssistOrderId(@Valid @RequestBody AddAssistOrderRequest request) {
        Integer userId = this.getCurrentUser().getId();
        request.setUserId(userId);
        return evaluationServive.addAssistOrderId(request);
    }

    /**
     * app获取助力单集合
     */
    @GetMapping("getAssistOrderList")
    public CommonResult getAssistOrderList() {
        Integer userId = null;
        try {
            userId = this.getCurrentUser().getId();
        } catch (Exception e) {
            return CommonResult.success();
        }
        return CommonResult.success(evaluationServive.getAssistOrderList(userId));
    }

    /**
     * 用户协助助力单
     * @return
     */
    @PostMapping("/userAssistHelp")
    public CommonResult userAssistHelp(@RequestBody UserHelpRequest userHelpRequest) {
        log.info("===userHelpRequest==={}", userHelpRequest);
        Integer userHelpWorkVo = evaluationServive.userHelpRequestList(userHelpRequest)== null ? 0 : evaluationServive.userHelpRequestList(userHelpRequest);
        log.info("====userHelpWorkVo==={}", userHelpWorkVo);
        int userHelpWork = evaluationServive.userHelpRequest(userHelpRequest);
        log.info("===userHelpWork==={}", userHelpWork);
        HelpWork helpWork = evaluationServive.selectHelpId(userHelpRequest.getHelpWorkId());
        log.info("userHelpRequest.getHelpWorkId()-------------->{}",userHelpRequest.getHelpWorkId());
        log.info("helpWork-------------->{}",helpWork);
        //自己不能给自己助力
        if (helpWork.getUserId().equals(userHelpRequest.getUserId())) {
            return CommonResult.success(HelpEnums.ONESELF_NO_HELP.getCode());
        }
        Integer userHelp = evaluationServive.userHelpa(userHelpRequest) == null ? 0 : evaluationServive.userHelpa(userHelpRequest);
        if(helpWork.getStatus()==2) {
            return CommonResult.success(HelpEnums.HELP_OSE_EFFICACY.getCode());
        }else{
            if(userHelp.equals(userHelpRequest.getHelpWorkId()) || userHelp.equals(userHelpRequest.getHelpWorkId())){
                return CommonResult.success(HelpEnums.ONESELF_NO_HELP.getCode());
            }else if(userHelpWork>=1){
                if (userHelpWorkVo.equals(userHelpRequest.getHelpWorkId())) {
                    return CommonResult.success(HelpEnums.ALREADY_HELP.getCode());
                }else{
                    return CommonResult.success(HelpEnums.NEW_USER.getCode());
                }
            }else{
                if(evaluationServive.insertUserHelps(userHelpRequest)==1) {
                    helpWork = evaluationServive.selectHelps(userHelpRequest.getHelpWorkId());
                    Integer a = helpWork.getHelpNum();
                    Integer b = helpWork.getHelpNumKo();
                    if (a == b + 1) {
                        evaluationServive.userAssistHelp(userHelpRequest, 1);
                        return CommonResult.success(HelpEnums.ESTABLISH_ORDER.getCode());
                    } else if (a == b){
                        return CommonResult.success(HelpEnums.HELP_FINISH.getCode());
                    }else{
                        evaluationServive.userAssistHelp(userHelpRequest,2);
                        return CommonResult.success(HelpEnums.HELP_SUCCEED.getCode());
                    }
                }
            }
        }
        return CommonResult.success();
    }
}
